import { config } from '../../config.js';
import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const app = getApp();
const my = new MyModel();
const common = new Common();

Page({
  data: {
    companyScale: config.data.companyScale, // 公司规模模板
    compType: config.data.compType, // 公司性质
    entryTime: null, // 入职时间
    quitTime: null, // 离职时间
    compName: null, // 公司名称
    posName: null, // 职位
    compNature: null, // 公司性质
    compScale: null, // 公司规模
    workText: null // 工作内容
  },
  // 保存
  saveWorkTime(e) {
    let value = e.detail.value;
    let data = {
      resumeId: common.getRid(),
      entryTime: this.data.entryTime,
      leaveTime: this.data.quitTime,
      workCompanyName: value.compName,
      workPosition: value.posName,
      companyNature: this.data.compNature,
      companyScale: this.data.compScale,
      workContent: value.workText
    };
    console.log(data)
    // 判断是否为空
    if(data['entryTime'] == null) {
      wx.showToast({
        title: '入职时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['leaveTime'] == null) {
      wx.showToast({
        title: '离职时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workCompanyName'] == null || data['workCompanyName'].trim() == '') {
      wx.showToast({
        title: '公司名称不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workPosition'] == null || data['workPosition'].trim() == '') {
      wx.showToast({
        title: '职位不能为空',
        icon: 'none'
      });
      return false
    }else if(data['companyNature'] == null) {
      wx.showToast({
        title: '公司性质不能为空',
        icon: 'none'
      });
      return false
    }else if(data['companyScale'] == null) {
      wx.showToast({
        title: '公司规模不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workContent'] == null || data['workContent'].trim() == '') {
      wx.showToast({
        title: '工作内容不能为空',
        icon: 'none'
      });
      return false
    }
    if(data['entryTime'] > data['leaveTime']) {
      wx.showToast({
        title: '入职时间不能大于离职时间!',
        icon: 'none'
      });
      return false
    }
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.showModal({
            title: '提示',
            content: '请先授权',
            success: function (res) {
              if (res.confirm) {
                app.getLoginCode();
                wx.redirectTo({
                  url: '/pages/login/index',
                })
              } else if (res.cancel) {
                console.log('这个用户不想授权')
              }
            }
          })
        }else {
          console.log(data)
          my.getResumes()
          .then(res => {
            if(res.data != null) {
              my.addHistory(data)
              .then(res => {
                console.log(res)
                if(res.code == 2) {
                  wx.showToast({
                    title: '添加成功',
                    icon: 'none',
                    duration: 2000
                  });
                  setTimeout(() => {
                    wx.redirectTo({
                      url: '../index/index'
                    });
                  }, 2000)
                }else {
                  wx.showToast({
                    title: '添加失败，请稍后重试！',
                    icon: 'none',
                    duration: 2000
                  });
                }
              })
            }else {
              wx.showModal({
                title: "请先创建简历",
                content: "是否前去创建简历",
                mask: true,
                success: res => {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: "/pages/my/createresume/index"
                    });
                  }
                }
              });
            }
          })
        }
      }
    })
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 入职时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 离职时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
      // 公司性质
      case 'compnature':
        _this.setData({
          compNature: e.detail.value
        })
      break;
      // 公司规模
      case 'compscale':
        _this.setData({
          compScale: e.detail.value
        });
      break;
      default:
      break;
    }
  }
})